from pyecharts.charts import Page
import pandas as pd
import sys
sys.path.append("..//database")
from SqlWizard import *
from CmnFuncs import *

def run_black():
    '''
    本模块包含焦煤、焦炭、铁矿石、螺纹钢、热卷，玻璃, 关联品种PVC
    设计图表：
    动力煤
    1. 焦煤现货基差图；
    2. 焦煤月差图；
    3. 焦炭现货基差图；
    4. 焦炭月差图；
    5. 焦炭 / 焦煤 比价（近似于焦化利润）
    6. 铁矿石现货基差图；
    7. 铁矿石月差图；
    8. 铁矿石 / 螺纹 比价
    9. 螺纹钢现货基差图；
    10. 螺纹钢月差图；
    11. 热卷现货基差图；
    12.热卷月差图；
    13.螺卷价差；
    14. 玻璃现货基差图
    15. 玻璃月差图
    16. 螺纹 / 玻璃
    17. PVC / 玻璃
    '''

    a_list = date_checker_1()
    b_list = date_checker_2()

    zc_spot = read_sql_spot('spot_zc')
    zc_index = read_sql_fut_index('fut_index_zc')
    zc_ry = read_sql_ry('roll_yield_zc')
    zc_spdrdx = read_sql_spdrdx('spdrdex_zc')
    zc_a1 = read_sql_fut('fut_zc{}'.format(a_list[0]))
    zc_a2 = read_sql_fut('fut_zc{}'.format(a_list[1]))
    zc_a3 = read_sql_fut('fut_zc{}'.format(a_list[2]))

    jm_spot = read_sql_spot('spot_jm')
    jm_index = read_sql_fut_index('fut_index_jm')
    jm_ry = read_sql_ry('roll_yield_jm')
    jm_spdrdx = read_sql_spdrdx('spdrdex_jm')
    jm_a1 = read_sql_fut('fut_jm{}'.format(a_list[0]))
    jm_a2 = read_sql_fut('fut_jm{}'.format(a_list[1]))
    jm_a3 = read_sql_fut('fut_jm{}'.format(a_list[2]))

    j_spot = read_sql_spot('spot_j')
    j_index = read_sql_fut_index('fut_index_j')
    j_ry = read_sql_ry('roll_yield_j')
    j_spdrdx = read_sql_spdrdx('spdrdex_j')
    j_a1 = read_sql_fut('fut_j{}'.format(a_list[0]))
    j_a2 = read_sql_fut('fut_j{}'.format(a_list[1]))
    j_a3 = read_sql_fut('fut_j{}'.format(a_list[2]))

    i_spot = read_sql_spot('spot_i')
    i_index = read_sql_fut_index('fut_index_i')
    i_ry = read_sql_ry('roll_yield_i')
    i_spdrdx = read_sql_spdrdx('spdrdex_i')
    i_a1 = read_sql_fut('fut_i{}'.format(a_list[0]))
    i_a2 = read_sql_fut('fut_i{}'.format(a_list[1]))
    i_a3 = read_sql_fut('fut_i{}'.format(a_list[2]))

    rb_spot = read_sql_spot('spot_rb')
    rb_index = read_sql_fut_index('fut_index_rb')
    rb_ry = read_sql_ry('roll_yield_rb')
    rb_spdrdx = read_sql_spdrdx('spdrdex_rb')
    rb_b1 = read_sql_fut('fut_rb{}'.format(b_list[0]))
    rb_b2 = read_sql_fut('fut_rb{}'.format(b_list[1]))
    rb_b3 = read_sql_fut('fut_rb{}'.format(b_list[2]))

    hc_spot = read_sql_spot('spot_hc')
    hc_index = read_sql_fut_index('fut_index_hc')
    hc_ry = read_sql_ry('roll_yield_hc')
    hc_spdrdx = read_sql_spdrdx('spdrdex_hc')
    hc_b1 = read_sql_fut('fut_hc{}'.format(b_list[0]))
    hc_b2 = read_sql_fut('fut_hc{}'.format(b_list[1]))
    hc_b3 = read_sql_fut('fut_hc{}'.format(b_list[2]))

    fg_spot = read_sql_spot('spot_fg')
    fg_index = read_sql_fut_index('fut_index_fg')
    fg_ry = read_sql_ry('roll_yield_fg')
    fg_spdrdx = read_sql_spdrdx('spdrdex_fg')
    fg_a1 = read_sql_fut('fut_fg{}'.format(a_list[0]))
    fg_a2 = read_sql_fut('fut_fg{}'.format(a_list[1]))
    fg_a3 = read_sql_fut('fut_fg{}'.format(a_list[2]))

    pvc_spot = read_sql_spot('spot_v')
    pvc_index = read_sql_fut_index('fut_index_v')
    pvc_a1 = read_sql_fut('fut_v{}'.format(a_list[0]))
    pvc_a2 = read_sql_fut('fut_v{}'.format(a_list[1]))
    pvc_a3 = read_sql_fut('fut_v{}'.format(a_list[2]))

    # 动力煤
    header_zc = draw_separator('动力煤')
    # 动力煤远期曲线
    future_curve_zc = draw_future_curve(zc_spot, zc_a1, zc_a2, zc_a3)
    # 动力煤一致性预期指标
    idctr_zc = draw_spec_idctr('动力煤', zc_spdrdx, zc_index, zc_ry)
    # 动力煤现货基差图；
    zc_spot_chart = draw_spot_spd('动力煤', zc_spot)
    # 动力煤合约价格、成交量、持仓量、持仓量变化图
    zc_contract_1 = draw_contract('zc{}'.format(a_list[0]), zc_a1)
    zc_contract_2 = draw_contract('zc{}'.format(a_list[1]), zc_a2)
    zc_contract_3 = draw_contract('zc{}'.format(a_list[2]), zc_a3)
    # 动力煤月差图；
    zc_12_chart = draw_spd('zc{}'.format(a_list[0]), zc_a1['close'], 'zc{}'.format(a_list[1]), zc_a2['close'])
    zc_23_chart = draw_spd('zc{}'.format(a_list[1]), zc_a2['close'], 'zc{}'.format(a_list[2]), zc_a3['close'])

    # 焦煤
    header_jm = draw_separator('焦煤')
    # 焦煤远期曲线
    future_curve_jm = draw_future_curve(jm_spot, jm_a1, jm_a2, jm_a3)
    # 焦煤一致性预期指标
    idctr_jm = draw_spec_idctr('焦煤', jm_spdrdx, jm_index, jm_ry)
    # 1. 焦煤现货基差图；
    jm_spot_chart = draw_spot_spd('焦煤', jm_spot)
    # 焦煤合约价格、成交量、持仓量、持仓量变化图
    jm_contract_1 = draw_contract('jm{}'.format(a_list[0]), jm_a1)
    jm_contract_2 = draw_contract('jm{}'.format(a_list[1]), jm_a2)
    jm_contract_3 = draw_contract('jm{}'.format(a_list[2]), jm_a3)
    # 2. 焦煤月差图；
    jm_12_chart = draw_spd('jm{}'.format(a_list[0]), jm_a1['close'], 'jm{}'.format(a_list[1]), jm_a2['close'])
    jm_23_chart = draw_spd('jm{}'.format(a_list[1]), jm_a2['close'], 'jm{}'.format(a_list[2]), jm_a3['close'])

    # 焦炭
    header_j = draw_separator('焦炭')
    # 焦炭远期曲线
    future_curve_j = draw_future_curve(j_spot, j_a1, j_a2, j_a3)
    # 焦炭一致性预期指标
    idctr_j = draw_spec_idctr('焦炭', j_spdrdx, j_index, j_ry)
    # 3. 焦炭现货基差图；
    j_spot_chart = draw_spot_spd('焦炭', j_spot)
    # 焦炭合约价格、成交量、持仓量、持仓量变化图
    j_contract_1 = draw_contract('j{}'.format(a_list[0]), j_a1)
    j_contract_2 = draw_contract('j{}'.format(a_list[1]), j_a2)
    j_contract_3 = draw_contract('j{}'.format(a_list[2]), j_a3)
    # 4. 焦炭月差图；
    j_12_chart = draw_spd('j{}'.format(a_list[0]), j_a1['close'], 'j{}'.format(a_list[1]), j_a2['close'])
    j_23_chart = draw_spd('j{}'.format(a_list[1]), j_a2['close'], 'j{}'.format(a_list[2]), j_a3['close'])

    # 铁矿石
    header_i = draw_separator('铁矿石')
    # 铁矿石远期曲线
    future_curve_i = draw_future_curve(i_spot, i_a1, i_a2, i_a3)
    # 铁矿石一致性预期指标
    idctr_i = draw_spec_idctr('铁矿石', i_spdrdx, i_index, i_ry)
    # 6. 铁矿石现货基差图；
    i_spot_chart = draw_spot_spd('铁矿石', i_spot)
    # 铁矿石合约价格、成交量、持仓量、持仓量变化图
    i_contract_1 = draw_contract('i{}'.format(a_list[0]), i_a1)
    i_contract_2 = draw_contract('i{}'.format(a_list[1]), i_a2)
    i_contract_3 = draw_contract('i{}'.format(a_list[2]), i_a3)
    # 7. 铁矿石月差图；
    i_12_chart = draw_spd('i{}'.format(a_list[0]), i_a1['close'], 'i{}'.format(a_list[1]), i_a2['close'])
    i_23_chart = draw_spd('i{}'.format(a_list[1]), i_a2['close'], 'i{}'.format(a_list[2]), i_a3['close'])

    # 螺纹
    header_rb = draw_separator('螺纹钢')
    # 螺纹远期曲线
    future_curve_rb = draw_future_curve(rb_spot, rb_b1, rb_b2, rb_b3)
    # 螺纹一致性预期指标
    idctr_rb = draw_spec_idctr('螺纹钢', rb_spdrdx, rb_index, rb_ry)
    # 9. 螺纹钢现货基差图；
    rb_spot_chart = draw_spot_spd('螺纹', rb_spot)
    # 螺纹钢合约价格、成交量、持仓量、持仓量变化图
    rb_contract_1 = draw_contract('rb{}'.format(b_list[0]), rb_b1)
    rb_contract_2 = draw_contract('rb{}'.format(b_list[1]), rb_b2)
    rb_contract_3 = draw_contract('rb{}'.format(b_list[2]), rb_b3)
    # 10. 螺纹钢月差图；
    rb_12_chart = draw_spd('rb{}'.format(b_list[0]), rb_b1['close'], 'rb{}'.format(b_list[1]), rb_b2['close'])
    rb_23_chart = draw_spd('rb{}'.format(b_list[1]), rb_b2['close'], 'rb{}'.format(b_list[2]), rb_b3['close'])

    # 热卷
    header_hc = draw_separator('热卷')
    # 热卷远期曲线
    future_curve_hc = draw_future_curve(hc_spot, hc_b1, hc_b2, hc_b3)
    # 热卷一致性预期指标
    idctr_hc = draw_spec_idctr('热卷', hc_spdrdx, hc_index, hc_ry)
    # 11. 热卷现货基差图；
    hc_spot_chart = draw_spot_spd('热卷', hc_spot)
    # 热卷合约价格、成交量、持仓量、持仓量变化图
    hc_contract_1 = draw_contract('hc{}'.format(b_list[0]), hc_b1)
    hc_contract_2 = draw_contract('hc{}'.format(b_list[1]), hc_b2)
    hc_contract_3 = draw_contract('hc{}'.format(b_list[2]), hc_b3)
    # 12.热卷月差图；
    hc_12_chart = draw_spd('hc{}'.format(b_list[0]), hc_b1['close'], 'hc{}'.format(b_list[1]), hc_b2['close'])
    hc_23_chart = draw_spd('hc{}'.format(b_list[1]), hc_b2['close'], 'hc{}'.format(b_list[2]), hc_b3['close'])

    # 玻璃
    header_fg = draw_separator('玻璃')
    # 玻璃远期曲线
    future_curve_fg = draw_future_curve_fg(fg_spot, fg_a1, fg_a2, fg_a3)
    # 玻璃一致性预期指标
    idctr_fg = draw_spec_idctr('玻璃', fg_spdrdx, fg_index, fg_ry)
    # 15. 玻璃现货基差图
    fg_spot_chart = draw_spot_spd_fg('玻璃', fg_spot)
    # 玻璃合约价格、成交量、持仓量、持仓量变化图
    fg_contract_1 = draw_contract('fg{}'.format(a_list[0]), fg_a1)
    fg_contract_2 = draw_contract('fg{}'.format(a_list[1]), fg_a2)
    fg_contract_3 = draw_contract('fg{}'.format(a_list[2]), fg_a3)
    # 16. 玻璃月差图
    fg_12_chart = draw_spd('fg{}'.format(a_list[0]), fg_a1['close'], 'fg{}'.format(a_list[1]), fg_a2['close'])
    fg_23_chart = draw_spd('fg{}'.format(a_list[1]), fg_a2['close'], 'fg{}'.format(a_list[2]), fg_a3['close'])

    # 比价
    header_cross = draw_separator('品种间')
    # 5. 焦炭 / 焦煤 比价（近似于焦化利润）
    arbi_record('j', j_index['close'], 'jm', jm_index['close'], x=1, y=1, opt=1, cat=1)
    arbi_record('j', j_spot['spot'], 'jm', jm_spot['spot'], x=1, y=1, opt=1, cat=2)
    ratio_j_jm_spot = draw_price_ratio('焦炭现货', j_spot['spot'], '焦煤现货', jm_spot['spot'], 4)
    ratio_j_jm_main = draw_price_ratio('焦炭主连', j_spot['main_price'], '焦煤主连', jm_spot['main_price'], 4)
    ratio_j_jm_1 = draw_price_ratio('j{}'.format(a_list[0]), j_a1['close'], 'jm{}'.format(a_list[0]), jm_a1['close'], 4)
    ratio_j_jm_2 = draw_price_ratio('j{}'.format(a_list[1]), j_a2['close'], 'jm{}'.format(a_list[1]), jm_a2['close'], 4)
    ratio_j_jm_3 = draw_price_ratio('j{}'.format(a_list[2]), j_a3['close'], 'jm{}'.format(a_list[2]), jm_a3['close'], 4)
    # 8. 铁矿石 / 螺纹 比价
    arbi_record('i', i_index['close'], 'rb', rb_index['close'], x=1, y=1, opt=1, cat=1)
    arbi_record('i', i_spot['spot'], 'rb', rb_spot['spot'], x=1, y=1, opt=1, cat=2)
    ratio_i_rb_spot = draw_price_ratio('铁矿石现货', i_spot['spot'], '螺纹现货', rb_spot['spot'], 1)
    ratio_i_rb_main = draw_price_ratio('铁矿石主连', i_spot['main_price'], '螺纹主连', rb_spot['main_price'], 1)
    ratio_i_rb_1 = draw_price_ratio('i{}'.format(a_list[0]), i_a1['close'], 'rb{}'.format(b_list[0]), rb_b1['close'], 1)
    ratio_i_rb_2 = draw_price_ratio('i{}'.format(a_list[1]), i_a2['close'], 'rb{}'.format(b_list[1]), rb_b2['close'], 1)
    ratio_i_rb_3 = draw_price_ratio('i{}'.format(a_list[2]), i_a3['close'], 'rb{}'.format(b_list[2]), rb_b3['close'], 1)

    # 13.螺卷价差；
    arbi_record('rb', rb_index['close'], 'hc', hc_index['close'], x=1, y=1, opt=2, cat=1)
    arbi_record('rb', rb_spot['spot'], 'hc', hc_spot['spot'], x=1, y=1, opt=2, cat=2)
    dif_rb_hc_spot = draw_price_dif('螺纹现货', rb_spot['spot'], '热卷现货', hc_spot['spot'], 5)
    dif_rb_hc_main = draw_price_dif('螺纹主连', rb_spot['main_price'], '热卷主连', hc_spot['main_price'], 5)
    dif_rb_hc_1 = draw_price_dif('rb{}'.format(b_list[0]), rb_b1['close'], 'hc{}'.format(b_list[0]), hc_b1['close'], 5)
    dif_rb_hc_2 = draw_price_dif('rb{}'.format(b_list[1]), rb_b2['close'], 'hc{}'.format(b_list[1]), hc_b2['close'], 5)
    dif_rb_hc_3 = draw_price_dif('rb{}'.format(b_list[2]), rb_b3['close'], 'hc{}'.format(b_list[2]), hc_b3['close'], 5)

    # 17. 螺纹 / 玻璃
    arbi_record('rb', rb_index['close'], 'fg', fg_index['close'], x=1, y=1, opt=1, cat=1)
    arbi_record('rb', rb_spot['spot'], 'fg', fg_spot['spot'], x=1, y=1, opt=1, cat=2)
    ratio_rb_fg_spot = draw_price_ratio('螺纹现货', rb_spot['spot'], '玻璃现货', fg_spot['spot'], 4)
    ratio_rb_fg_main = draw_price_ratio('螺纹主连', rb_spot['main_price'], '玻璃主连', fg_spot['main_price'], 4)
    ratio_rb_fg_1 = draw_price_ratio('rb{}'.format(b_list[0]), rb_b1['close'],
                                     'fg{}'.format(a_list[0]), fg_a1['close'], 4)
    ratio_rb_fg_2 = draw_price_ratio('rb{}'.format(b_list[1]), rb_b2['close'],
                                     'fg{}'.format(a_list[1]), fg_a2['close'], 4)
    ratio_rb_fg_3 = draw_price_ratio('rb{}'.format(b_list[2]), rb_b3['close'],
                                     'fg{}'.format(a_list[2]), fg_a3['close'], 4)
    # 18. PVC / 玻璃
    arbi_record('pvc', pvc_index['close'], 'fg', fg_index['close'], x=1, y=1, opt=1, cat=1)
    arbi_record('pvc', pvc_spot['spot'], 'fg', fg_spot['spot'], x=1, y=1, opt=1, cat=2)
    ratio_pvc_fg_spot = draw_price_ratio('PVC现货', pvc_spot['spot'], '玻璃现货', fg_spot['spot'], 1)
    ratio_pvc_fg_main = draw_price_ratio('PVC主连', pvc_spot['main_price'], '玻璃主连', fg_spot['main_price'], 1)
    ratio_pvc_fg_1 = draw_price_ratio('pvc{}'.format(a_list[0]), pvc_a1['close'],
                                      'fg{}'.format(a_list[0]), fg_a1['close'], 1)
    ratio_pvc_fg_2 = draw_price_ratio('pvc{}'.format(a_list[1]), pvc_a2['close'],
                                      'fg{}'.format(a_list[1]), fg_a2['close'], 1)
    ratio_pvc_fg_3 = draw_price_ratio('pvc{}'.format(a_list[2]), pvc_a3['close'],
                                      'fg{}'.format(a_list[2]), fg_a3['close'], 1)

    page = Page(page_title='黑色链', layout=Page.SimplePageLayout)
    page.add(
        header_zc,
        future_curve_zc,
        idctr_zc,
        zc_spot_chart,
        zc_contract_1,
        zc_contract_2,
        zc_contract_3,
        zc_12_chart,
        zc_23_chart,
        header_jm,
        future_curve_jm,
        idctr_jm,
        jm_spot_chart,
        jm_contract_1,
        jm_contract_2,
        jm_contract_3,
        jm_12_chart,
        jm_23_chart,
        header_j,
        future_curve_j,
        idctr_j,
        j_spot_chart,
        j_contract_1,
        j_contract_2,
        j_contract_3,
        j_12_chart,
        j_23_chart,
        header_i,
        future_curve_i,
        idctr_i,
        i_spot_chart,
        i_contract_1,
        i_contract_2,
        i_contract_3,
        i_12_chart,
        i_23_chart,
        header_rb,
        future_curve_rb,
        idctr_rb,
        rb_spot_chart,
        rb_contract_1,
        rb_contract_2,
        rb_contract_3,
        rb_12_chart,
        rb_23_chart,
        header_hc,
        future_curve_hc,
        idctr_hc,
        hc_spot_chart,
        hc_contract_1,
        hc_contract_2,
        hc_contract_3,
        hc_12_chart,
        hc_23_chart,
        header_fg,
        future_curve_fg,
        idctr_fg,
        fg_spot_chart,
        fg_contract_1,
        fg_contract_2,
        fg_contract_3,
        fg_12_chart,
        fg_23_chart,
        header_cross,
        ratio_j_jm_spot,
        ratio_j_jm_main,
        ratio_j_jm_1,
        ratio_j_jm_2,
        ratio_j_jm_3,
        ratio_i_rb_spot,
        ratio_i_rb_main,
        ratio_i_rb_1,
        ratio_i_rb_2,
        ratio_i_rb_3,
        dif_rb_hc_spot,
        dif_rb_hc_main,
        dif_rb_hc_1,
        dif_rb_hc_2,
        dif_rb_hc_3,
        ratio_rb_fg_spot,
        ratio_rb_fg_main,
        ratio_rb_fg_1,
        ratio_rb_fg_2,
        ratio_rb_fg_3,
        ratio_pvc_fg_spot,
        ratio_pvc_fg_main,
        ratio_pvc_fg_1,
        ratio_pvc_fg_2,
        ratio_pvc_fg_3
    )
    page.render('黑色链.html')

